package com.xlg.file.center.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanMap;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: 18310740596@163.com
 * @date: 2023-07-15 00:07:16
 * @version: v1.0
 * @describe: class responsibility
 */
@Slf4j
public class CsvFileHandler extends AbstractFileHandler {

    private static final String FILE_SUFFIX = ".csv";

    @Override
    public void writeTitle(Handler handler) throws Exception {

        File file = super.createFile(handler, FILE_SUFFIX);

        String title = String.join(",", handler.getTask().getTitles());

        this.appendContent(file, title);

        handler.setFile(file);
    }

    private void appendContent(File file, String content) throws IOException {
        try (FileOutputStream fileOutputStream = new FileOutputStream(file, true);
             OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream)) {
            outputStreamWriter.append(content);
            outputStreamWriter.append(System.lineSeparator());
        } catch (IOException e) {
            log.error("文件追加内容失败.");
            throw e;
        }
    }

    @Override
    public void writeContent(Handler handler) throws Exception {
        File file = handler.getFile();

        try (FileOutputStream fileOutputStream = new FileOutputStream(file, true);
             OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream)) {

            handler.getTask().getContents()
                    .parallelStream()
                    .forEach(obj -> {
                        // 转换为 map 对象
                        BeanMap beanMap = BeanMap.create(obj);
                        List<String> content = new ArrayList<>();

                        try {
                            handler.getTask().getTitles()
                                    .stream()
                                    .map(String::valueOf)
                                    .forEach(title -> content.add(Objects.isNull(beanMap.get(title)) ? "" : String.valueOf(beanMap.get(title))));

                            synchronized (outputStreamWriter) {
                                outputStreamWriter.append(String.join(",", content));
                                outputStreamWriter.append(System.lineSeparator());
                            }
                        } catch (IOException e) {
                            handler.getMsg().append("文件唯一批次号：")
                                    .append(handler.getTask().getUniqNo())
                                    .append("  批次号:[")
                                    .append(handler.getTask().getBatchNo())
                                    .append("] 写入文件错误。")
                                    .append(System.lineSeparator());
                        }
                    });
        } catch (IOException e) {
            log.error("文件追加内容失败.");
            throw e;
        }
    }
}
